// pages/comment/addcomment.js
const App = getApp();
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		order_id: 0,
		goodsList: [],
		image_list: [],
		imageIndex: 0,
		current: 5,
		formData: [],
		submitDisable: false,
		is_anonymous: 0,
		isLoading: true,
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(options) {
		let _this = this;
		_this.setData({
			order_id: options.order_id,
		});
		_this.getOrderData();
	},
	getOrderData() {
		let _this = this;
		let postData = {
            order_id: _this.data.order_id
		};

		App._requestApi(_this, App.globalData.config.shopOrderCommentUrl, postData, function(res) {
			let formData = _this.data.formData;
			res.data.detail.goods.forEach(function(item, formIndex) {
				formData.push({
					order_code: res.data.detail.order_code,
					order_id: _this.data.order_id,
					details_id: item.details_id,
					product_id: item.product_id,
					litpic: item.litpic,
					title: item.product_name,
					total_score: 5,
					content: '',
					upload_img: [],
					upload_img_arr: []
				});
			});
			_this.setData({
				isLoading: false,
				config: res.data.config,
				formData: formData
			});
		});
	},

	// 改变星星的状态
	change(e) {
		let _this = this,
			index = e.currentTarget.dataset.index;
		_this.setData({
			['formData[' + index + '].total_score']: e.detail.index
		});
	},
	inputChange(e) {
		let _this = this,
			index = e.currentTarget.dataset.index;
		_this.setData({
			['formData[' + index + '].content']: e.detail.value
		});
	},
	changeAnonymous(e) {
		let _this = this,
			value = e.detail.value[0],
			is_anonymous = 0;
		if (1 == value) {
			is_anonymous = 1;
		}

		_this.setData({
			is_anonymous: is_anonymous
		});
	},
	submitComment(e) {
		let _this = this,
			formData = _this.data.formData,
			imagesLength = 0;

		// 判断是否重复提交
		if (_this.data.submitDisable === true) return false;
		// 表单提交按钮设为禁用 (防止重复提交)
		_this.setData({
			submitDisable: true
		});
		formData.forEach(function(item, formIndex) {
			imagesLength += item.upload_img_arr.length;
		});

		let fromPostCall = function(formData) {
			formData = JSON.stringify(formData)
			App._requestPost(_this, App.globalData.config.shopSaveCommentUrl, {
				formData: formData,
				is_anonymous: _this.data.is_anonymous,
				order_id: _this.data.order_id
			}, function(res) {
				App.showSuccess(res.msg, function() {
					wx.navigateBack();
				});
			});
		}

		// 判断是否需要上传图片
		imagesLength > 0 ? _this.uploadFile(formData, imagesLength, fromPostCall) : fromPostCall(formData);
	},

	// 上传图片
	uploadFile: function(formData, imagesLength, callBack) {
		wx.showLoading({
			title: '图片上传中',
			mask: true
		});
		// 文件上传
		var addSuccess = 0;
		formData.forEach(function(i, idx) {
			if (0 < i.upload_img_arr.length) {
				i.upload_img_arr.forEach(function(item, index) {
					wx.uploadFile({
						url: App.globalData.config.shopUploadsUrl,
						filePath: item,
						name: 'file',
						success: function(res) {
							let result = typeof res.data === "object" ? res
								.data : JSON.parse(res.data);
							if (result.code === 1) {
								addSuccess++;
								i.upload_img.push(result.data.img_url);
							}
							if (addSuccess == imagesLength) {
								// 所有文件上传完成，执行回调函数
								callBack && callBack(formData);
							}
						}
					});
				})
			}
		});
	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {

	},

	/**
	 * 选择图片
	 */
	chooseImage(e) {
		let _this = this,
			index = e.currentTarget.dataset.index,
			upload_img = _this.data.formData[index].upload_img_arr;
		wx.chooseMedia({
			mediaType: ['image'],
			sourceType: ['album', 'camera'],
			sizeType: ['original', 'compressed'],
			count: 9 - upload_img.length,
			success: function(res) {
				res.tempFiles.forEach(function(item, formIndex) {
					upload_img.push(item.tempFilePath);
				});
				_this.setData({
					['formData[' + index + '].upload_img_arr']: upload_img
				});
			}
		});
	},

	/**
	 * 删除图片
	 */
	deleteImage: function(e) {
		let _this = this,
			index = e.currentTarget.dataset.index,
			imageIndex = e.currentTarget.dataset.imageIndex,
			upload_img = _this.data.formData[index].upload_img_arr;

		upload_img.splice(imageIndex, 1);
		_this.setData({
			['formData[' + index + '].upload_img_arr']: upload_img
		})
	},


	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {

	}
})
